package service

import (
	"bc/dao"
	"bc/util/library/zstack"
	"encoding/json"
	"fmt"
)

type l3_net struct {

}
func (t *l3_net)Create(d map[string]interface{}) bool {

	return dao.L3Net.Create(d)

}
func (t *l3_net)Delete(w map[string]interface{}) bool {
	return dao.L3Net.Delete(w)
}
/**
创建三层网络
 */
func (t *l3_net)CreateL3Network(name string, l2NetworkUuid string, category string) string {
	//创建三层网络
	l3Map := map[string]interface{}{}
	l3Map["params"] = make(map[string]interface{})
	l3Map["params"].(map[string]interface{})["name"] = name
	l3Map["params"].(map[string]interface{})["type"] = "L3BasicNetwork"
	l3Map["params"].(map[string]interface{})["l2NetworkUuid"] = l2NetworkUuid
	l3Map["params"].(map[string]interface{})["category"] = category
	l3Map["params"].(map[string]interface{})["system"] = false

	l3_str,err := json.Marshal(l3Map)
	if err != nil {
		fmt.Println("三层网络json数据错误")
		return ""
	}

	l3ResStr, err := zstack.NetWork.CreateL3Network(string(l3_str))
	inventory := map[string]interface{}{}
	for {
		res := zstack.NetWork.GetResults(l3ResStr)
		if _,ok := res["error"];ok {
			fmt.Println(res["error"])
			break
		}
		if v,ok := res["inventory"];ok {
			fmt.Println("创建三层网络成功")
			inventory = v.(map[string]interface{})
			break
		}
	}
	return inventory["uuid"].(string)
}
var L3Net = &l3_net{}
